<?php
namespace Admin\Model;
use Think\Model;

class MealModel extends Model
{
    /* 自动验证规则 */
    protected $_validate = array(
        array('title', 'require', '标题必须', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('start_date', 'require', '开始时间必须', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('end_date', 'require', '结束时间必须', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    );
    
    /* 自动完成规则 */
    protected $_auto = array(
    	array('create_time', "getCurTime", self::MODEL_INSERT, 'callback'),
        array('update_time', "getCurTime", self::MODEL_BOTH, 'callback'),
    );
    
    protected function getCurTime()
    {
        return date('Y-m-d H:i:s');
    }

    public function getCatList($meal_id) {
        $meal_id = (int)$meal_id;
        $list = array();
        $sql = "SELECT B.* FROM `yiyou_meal_cats` A LEFT JOIN `yiyou_meal_category` B ON A.cat_id = B.id WHERE `meal_id` = $meal_id ";
        $res = $this->query($sql);
        foreach ($res as $item) {
            $list[$item['id']] = $item;
        }
        return $list;
    }

    public function getInfo($id)
    {
        $info = $this->find($id);
        if ($info) {
            $info['meal_cats'] = $this->getCatList($info['id']);
        }
        return $info;
    }
}